   package com.javalong.affairs.module.schedule.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;

import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.base.common.ServiceLocator;
import com.javalong.affairs.base.common.UIHandler;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestCode;
import com.javalong.affairs.base.fragment.BaseFragment;
import com.javalong.affairs.module.notifything.fragment.DeptFragment;
import com.javalong.affairs.module.schedule.fragment.ScheduleListFragment;
import com.javalong.affairs.module.task.fragment.TaskSearchFragment;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.Dept;
import com.javalong.affairs.module.notifything.activity.DeptCheckActivity;
import com.javalong.affairs.base.ui.BottomBar;
import com.javalong.affairs.base.ui.BottomBar.OnBottomClick;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.ui.TopTitle.OnLeftDrawableClick;
import com.javalong.affairs.base.ui.TopTitle.OnRightDrawableClick;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_notification)
public class ScheduleActivity extends UIHandlerActivity implements OnBottomClick,
		OnRightDrawableClick {

	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.ui_bottombar)
	private BottomBar bottomBar;
	@ViewInject(R.id.ll_root)
	private LinearLayout ll_root;
	private ScheduleListFragment scheduleListFragment;
	private DeptFragment deptFragment;
	private BaseFragment currFragment;
	private int[] drawables = new int[] { R.drawable.schedule_level_f1,
			R.drawable.schedule_level_f2, R.drawable.schedule_level_f3 };
	private PopupWindow pwMyPopWindow;
	private ArrayList<Map<String, String>> moreList;
	private ListView lvPopupList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		topTitle.setOnLeftDrawableClick(new OnLeftDrawableClick() {

			@Override
			public void leftClick() {
				onBackPressed();
			}
		});
		topTitle.setOnRightDrawableClick(this);
		topTitle.setTitle("个人日程");
		bottomBar.setOnBottonClick(this);
		bottomBar.setRes(drawables, null);
		bottomBar.setImageSize(60);
		loadOneFragment();
		iniData();
		iniPopupWindow();
	}

	private void iniData() {
		moreList = new ArrayList<Map<String, String>>();
		Map<String, String> map;
		map = new HashMap<String, String>();
		map.put("share_key", "添加个人日程");
		moreList.add(map);
		map = new HashMap<String, String>();
		map.put("share_key", "个人日程统计");
		moreList.add(map);
		map = new HashMap<String, String>();
		map.put("share_key", "回收站");
		moreList.add(map);

	}

	private void iniPopupWindow() {
		LayoutInflater inflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.popwindow_task, null);
		lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
		pwMyPopWindow = new PopupWindow(layout);
		pwMyPopWindow.setFocusable(true);// 加上这个popupwindow中的ListView才可以接收点击事件
		lvPopupList.setAdapter(new SimpleAdapter(this, moreList,
				R.layout.item_popwindow, new String[] { "share_key" },
				new int[] { R.id.tv_list_item }));
		lvPopupList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				case 0:
					IntentManager.doActivity(ScheduleActivity.this,
							MyScheduleActivity.class);
					break;
				case 1:
					IntentManager.doActivity(ScheduleActivity.this,
							DeptCheckActivity.class);
					break;
				case 2:
					IntentManager.doActivity(ScheduleActivity.this,
							ScheduleRecycleActivity.class);
					break;
				}
			}
		});
		// 控制popupwindow的宽度和高度自适应
		lvPopupList.measure(View.MeasureSpec.UNSPECIFIED,
				View.MeasureSpec.UNSPECIFIED);
		pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
		pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight()) * 3);
		// 控制popupwindow点击屏幕其他地方消失
		pwMyPopWindow.setBackgroundDrawable(new BitmapDrawable());
		pwMyPopWindow.setOutsideTouchable(true);// 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上
	}

	// 载入首视图
	private void loadOneFragment() {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		if (scheduleListFragment == null) {
			scheduleListFragment = new ScheduleListFragment();
		}
		ft.add(R.id.fl_notify, scheduleListFragment);
		ft.commit();
		bottomBar.setSelection(0);
	}

	@Override
	public void onBottomClick(int index) {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		currFragment = null;
		switch (index) {
		case 0:
			if (scheduleListFragment == null) {
				scheduleListFragment = new ScheduleListFragment();
			}
			currFragment = scheduleListFragment;
			break;
		case 1:
			if (deptFragment == null) {
				deptFragment = new DeptFragment();
			}
			currFragment = deptFragment;
			break;
		case 2:
			currFragment = new TaskSearchFragment();
			Bundle bundle = new Bundle();
			bundle.putBoolean(BundleKey.ISSCHEDULE, true);
			currFragment.setArguments(bundle);

			break;
		}
		ft.replace(R.id.fl_notify, currFragment);
		ft.commit();
	}

	@Override
	public void rightClick() {
		if (pwMyPopWindow.isShowing()) {
		} else {
			pwMyPopWindow.showAsDropDown(topTitle.getIv_right_drawable());
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case RequestCode.DEPTLIST:
				List<Dept> deptList = (List<Dept>) data
						.getSerializableExtra(BundleKey.DEPTLIST);
				Bundle bundle = new Bundle();
				bundle.putSerializable(BundleKey.DEPTLIST,
						(Serializable) deptList);
				UIHandler uiHandler = ServiceLocator.GetInstance().getInstance(
						UIHandler.class);
				uiHandler.postCallback(R.string.event_add_dept, bundle);
				break;
			}
		}
	}

}
